package com.mit.web.business;

import java.util.Date;
import java.util.List;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.purchase.dto.ColorStyleDTO;
import com.mit.common.purchase.dto.InquiryItemDTO;
import com.mit.common.purchase.dto.InquiryTaskDTO;
import com.mit.common.purchase.dto.MaterialDTO;
import com.mit.common.purchase.dto.PODTO;
import com.mit.common.purchase.dto.POItemDTO;
import com.mit.common.purchase.dto.SupplierDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.DesignTokens;
import com.mit.common.token.PurchaseTokens;
import com.mit.common.token.Tokens;

public class PurchaseService extends RemoteService {
	
	private final String FACADE_NAME = "PurchaseFacade";
	@Override
	public String getFacadeName() {
		
		return FACADE_NAME;
	}

	public SupplierDTO saveSupplier(SupplierDTO to,UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveSupplier", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_DTO, to);
		ResultContext rlt  = ServiceLocator.getInstance().doBusiness(rqs);	
		SupplierDTO rtn = (SupplierDTO) rlt.getResult(PurchaseTokens.KEY_SUPPLIER_DTO);
		return rtn;

	}
	
	public List<SupplierDTO> retrievePagedSuppliers(String searchKey, Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedSuppliers", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_NAME, searchKey);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<SupplierDTO> rtn = (List<SupplierDTO>) rlt.getResult(PurchaseTokens.KEY_SUPPLIER_LIST);
		return rtn;
	}
	
	public Integer retrieveSupplierCount(String searchKey, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveSupplierCount", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_NAME, searchKey);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public List<SupplierDTO> retrieveSupplierBasicInfo(String name, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveSuppliersBasicInfo", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_NAME, name);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<SupplierDTO> rtn = (List<SupplierDTO>) rlt.getResult(PurchaseTokens.KEY_SUPPLIER_LIST);
		return rtn;
	}
	
	public List<MaterialDTO> retrieveMaterials(Integer supplierId, String activeIndicator, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveMaterials", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_ID, supplierId);
		rqs.addParameter(Tokens.KEY_ACTIVE_INDICATOR, activeIndicator);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<MaterialDTO> lst = (List<MaterialDTO>) rlt.getResult(PurchaseTokens.KEY_MATERIAL_LIST);
		return lst;
	}
	
	public MaterialDTO saveMaterial(MaterialDTO material, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveMaterial", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_MATERIAL_DTO, material);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		MaterialDTO rtn = (MaterialDTO) rlt.getResult(PurchaseTokens.KEY_MATERIAL_DTO);
		return rtn;
		
	}
	
	public List<ColorStyleDTO> retrieveColorStyles(Integer materialId, String activeIndicator, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveColorStyles", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_MATERIAL_ID, materialId);
		rqs.addParameter(Tokens.KEY_ACTIVE_INDICATOR, activeIndicator);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<ColorStyleDTO> lst = (List<ColorStyleDTO>) rlt.getResult(PurchaseTokens.KEY_COLOR_STYLE_LIST);
		return lst;
	}
	
	public ColorStyleDTO saveColorStyle(ColorStyleDTO colorStyle, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveColorStyle", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_COLOR_STYLE_DTO, colorStyle);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		ColorStyleDTO rtn = (ColorStyleDTO) rlt.getResult(PurchaseTokens.KEY_COLOR_STYLE_DTO);
		return rtn;
		
	}	
	
	public List<InquiryTaskDTO> retrievePagedInquiryTasks(String styleCode, String state
			, Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedInquiryTasks", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, styleCode);
		rqs.addParameter(Tokens.KEY_STATE, state);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		List<InquiryTaskDTO> lst = (List<InquiryTaskDTO>) rlt.getResult(PurchaseTokens.KEY_INQUIRY_TASK_LIST);
		return lst;
	}
	
	public Integer retrieveInquiryTaskCount(String styleCode, String state, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveInquiryTaskCount", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, styleCode);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public List<InquiryItemDTO> retrieveInquiryItems(Integer taskId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveInquiryItems", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_INQUIRY_TASK_ID, taskId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		List<InquiryItemDTO> lst = (List<InquiryItemDTO>) rlt.getResult(PurchaseTokens.KEY_INQUIRY_ITEM_LIST);
		return lst;
	}
	
	public InquiryItemDTO saveInquiryItem(InquiryItemDTO item, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveInquiryItem", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_INQUIRY_ITEM_DTO, item);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);
		return (InquiryItemDTO) rlt.getResult(PurchaseTokens.KEY_INQUIRY_ITEM_DTO);
	}
	
	public InquiryTaskDTO saveInquiryTask(InquiryTaskDTO task, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveInquiryTask", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_INQUIRY_TASK_DTO, task);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);
		task = (InquiryTaskDTO) rlt.getResult(PurchaseTokens.KEY_INQUIRY_TASK_DTO);
		return task;
	}
	
	public List<PODTO> retrievePagedPO(String styleCode, String code, Integer supplierId, Date dateFrom, 
			Date dateTo, String state, Integer pageNo, Integer pageSize, UserDTO currentUser)throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedPO", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_STYLE_CODE, styleCode);
		rqs.addParameter(PurchaseTokens.KEY_PO_CODE, code);
		if(supplierId > 0)
			rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_ID, supplierId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<PODTO> rtn = (List<PODTO>) rlt.getResult(Tokens.KEY_LIST);
		return rtn;
	}
	
	public Integer retrievePOCount(String styleCode, String code, Integer supplierId, Date dateFrom, 
			Date dateTo, String state, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePOCount", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_STYLE_CODE, styleCode);
		rqs.addParameter(PurchaseTokens.KEY_PO_CODE, code);
		if(supplierId > 0)
			rqs.addParameter(PurchaseTokens.KEY_SUPPLIER_ID, supplierId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public List<POItemDTO> retrievePendingPurchaseItems(UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePendingPurchaseItems", currentUser);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<POItemDTO> rtn = ( List<POItemDTO>) rlt.getResult(Tokens.KEY_LIST);
		return rtn;
	}
	
	public InquiryItemDTO retrieveInquiryInfo(Integer fabId, Integer prdColorId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveInquiryInfo", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_FAB_ID, fabId);
		if(prdColorId > 0)
			rqs.addParameter(PurchaseTokens.KEY_PRD_COLOR_ID, prdColorId);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		InquiryItemDTO rtn = (InquiryItemDTO) rlt.getResult(PurchaseTokens.KEY_INQUIRY_ITEM_DTO);
		return rtn;
	}
	
	public List<PODTO> bulkCreatePO(PODTO podto, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("bulkCreatePO", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_PO_DTO, podto);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return (List<PODTO>) rlt.getResult(Tokens.KEY_LIST);
	}
	
	public List<POItemDTO> retrieveMergedPOItems(Integer poId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveMergedPOItems", currentUser);
		rqs.addParameter(Tokens.KEY_ID, poId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return (List<POItemDTO>) rlt.getResult(Tokens.KEY_LIST);
	}
	
	public PODTO updatePOState(Integer poId, String state, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("updatePOState", currentUser);
		rqs.addParameter(Tokens.KEY_ID, poId);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return (PODTO) rlt.getResult(PurchaseTokens.KEY_PO_DTO);
	}
	
	public PODTO deletePO(Integer poId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("deletePO", currentUser);
		rqs.addParameter(Tokens.KEY_ID, poId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return (PODTO) rlt.getResult(PurchaseTokens.KEY_PO_DTO);
	
	}
	
	public List<POItemDTO> retrieveEditablePOItems(POItemDTO item, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveEditablePOItems", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_PO_ITEM_DTO, item);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return ( List<POItemDTO>) rlt.getResult(Tokens.KEY_LIST);
	}
	
	public PODTO savePO(PODTO po, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("savePO", currentUser);
		rqs.addParameter(PurchaseTokens.KEY_PO_DTO, po);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		return (PODTO) rlt.getResult(PurchaseTokens.KEY_PO_DTO);
	}
	
}
